package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * 和讯博客
 * @author Persh
 *
 */
public class HexunBlogAnalyse implements AnalyseNews{

	//http://ranitahe.blog.hexun.com/81610674_d.html

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://.*.blog.hexun.com/[0-9]*_d.html"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		String html = urlMeta.getHtml();
		if (html== null) {
		}
		NewsMeta blog = new NewsMeta();
		blog.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String author = null;
		Document doc = Jsoup.parse(html);
		title =doc.select("span.ArticleTitleText").text();
		author = doc.select("div.site_description_A").text();
		content = HtmlCleaner.getContentHtml(url, doc.select("div#BlogArticleDetail"));
		String tempdate=doc.select("div.ArticleTitle").text();
		date = DateUtils.matchDate(tempdate);
		blog.setTitle(StringUtils.trimSpace(title));
		blog.setContent(content);
		blog.setDate(date);
		blog.setAuthor(author);
		/**
		 * 解析更新路径 http://click.tool.hexun.com/click.aspx?articleid=81661172&blogid=13201885
		 */
		String updateUrl = null;
		try {
			String scriptStr = html.substring(html.lastIndexOf("<!--我顶后面的博主列表-->"));
			scriptStr = scriptStr.substring(scriptStr.indexOf("src=")+"src=".length(), scriptStr.indexOf("</script>"));
			updateUrl = scriptStr.substring(1, scriptStr.indexOf("\">"));
		} catch (Exception e) {
		}
		if(updateUrl!=null){
			blog.setUpdateUrl(updateUrl);
			NewsMeta update = Update(blog);
			return update;
		}
		return blog;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				String getHtml = CrawlHTML.GetHtml(updateUrl);
				if(getHtml!=null){
					Integer clickNum=0;
					Integer commentNum=0;
					try {
						String[] split = getHtml.split(";");
						String clickStr = split[0];
						String commentStr = split[1];
						clickStr = clickStr.substring(clickStr.indexOf("=")+1);
						commentStr = commentStr.substring(commentStr.indexOf("=")+1);
						clickNum = Integer.parseInt(clickStr.trim());
						commentNum = Integer.parseInt(commentStr.trim());
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
					}
					meta.setClickNum(clickNum);
					meta.setCommentNum(commentNum);
					return meta;
				}
			}
		}
		return null;
	}





	public static void main(String[] args) {
		String url = "http://jlq777888.blog.hexun.com/91144069_d.html";
		HexunBlogAnalyse test = new HexunBlogAnalyse();
		if(test.isDetailPage(url)){
			UrlMeta responseToURL = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = test.parserHtml(responseToURL);
			System.out.println(parserHtml);
		}else{
			System.out.println("不符合规则");
		}
	}

	
}
